package cn.wolfcode.springboot_producer.controller;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 乱梦天真
 * @Description: TODO
 * @Date: 2020/5/10 16:00
 */
@RestController
public class Controller {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    //TODO 同步发送消息
    @RequestMapping("/send")
    public Object send(String msg){
        rocketMQTemplate.syncSend("01_boot_hello",msg);
        return "消息发送成功";
    }

    //TODO 异步发送消息
    @RequestMapping("/asyncSend")
    public Object asyncSend(String msg){
        rocketMQTemplate.asyncSend("01_boot_hello", msg, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("发送成功");
            }
            @Override
            public void onException(Throwable throwable) {
                System.out.println("发送失败");
            }
        });
        return "消息发送成功";
    }

    //TODO 一次性发送消息
    @RequestMapping("/sendOneWay")
    public Object sendOneWay(String msg){
        rocketMQTemplate.sendOneWay("01_boot_hello",msg);
        return "消息发送成功";
    }

    //TODO 集群方式接受消息
    @RequestMapping("/sendWay")
    public Object sendWay(String msg){
        rocketMQTemplate.sendOneWay("02-boot-sendWay",msg);
        return "消息发送成功";
    }
    //TODO 广播方式接受消息
    @RequestMapping("/sendWay1")
    public Object sendWay1(String msg){
        rocketMQTemplate.sendOneWay("02-boot-sendWay1",msg);
        return "消息发送成功";
    }

    //TODO 延时发送消息
    @RequestMapping("/delay")
    public Object delay(String msg) throws Exception {
        //TODO 方式1
        MessageBuilder<String> builder = MessageBuilder.withPayload(msg);
        Message<String> message = builder.build();
        rocketMQTemplate.syncSend("03-boot-delay",message,3000,3);
        //TODO 方式2
        /*DefaultMQProducer producer = rocketMQTemplate.getProducer();
        Message message = new Message("03-boot-delay",msg.getBytes());
        message.setDelayTimeLevel(3);
        producer.send(message);*/
        return "消息发送成功";
    }

    //TODO Tag消息过滤
    @RequestMapping("/filter1")
    public Object filter1(String msg,String tag){
        rocketMQTemplate.syncSend("04-boot-filter:"+tag,msg);
        return "消息发送成功";
    }

    //TODO SQL92消息过滤
    @RequestMapping("/filter2")
    public Object filter2(String msg){
        Map<String,Object> map = new HashMap<>();
        map.put("age",18);
        map.put(MessageConst.PROPERTY_KEYS,111);
        rocketMQTemplate.convertAndSend("04-boot-filter1",msg,map);
        return "消息发送成功";
    }
}